package com.hxm.demo.test1;

import java.util.HashMap;
import java.util.Map;

/**
 * @author huangxiaoming
 * @description
 * @date 2022/6/11 17:29
 */
public class TestTwoSum {

    /**
     * 两数之和
     * @param args
     */
    public static void main(String[] args) {
        int[] nums = new int[]{5,8,9,7};
        int[] ints = twoSum(nums, 17);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }

    public static int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        Map<Integer,Integer> map = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])){
                result[0]=map.get(nums[i]);
                result[1] = i;
            } else {
                map.put(target-nums[i],i);
            }
        }
        return result;
    }
}
